using UnityEngine;
using System.Collections;

public class MathHelper {

	/// <summary>
	/// This is similar to Mathf.Approximately, but with a tolerance
	/// </summary>
	/// <param name="a"></param>
	/// <param name="b"></param>
	/// <param name="tolerance"></param>
	/// <returns></returns>
	public static bool Approximately(float a, float b, float tolerance) {
		float fDiff = Mathf.Abs(a - b);
		if (fDiff > tolerance) {
			return false;
		}
		return true;
	}
	

	/// <summary>
	/// Gives an angle between two vectors between 0 and 360, clockwise.
	/// </summary>
	/// <param name="FromVector"></param>
	/// <param name="ToVector"></param>
	/// <returns></returns>
	public static float AngleDifferance(Vector2 FromVector,Vector2 ToVector) {

    	float result = Vector2.Angle( FromVector, ToVector );
   		Vector3 cross = Vector3.Cross( FromVector, ToVector );

    	if (cross.z > 0) {
       		result = 360f - result;
		}

    	return result;
		
	}



	/// <summary>
	/// Dim an object material from its current color to the specified color
	/// Dim duration needs to be specified
	/// Call with StartCoroutine()
	/// </summary>
	/// <param name="dimObj">The object of which to dim its material color</param>
	/// <param name="dimColor">The final dim color</param>
	/// <param name="lerpTime">The dim duration, e.g. 1.0f = 1 second</param>
	/// <returns></returns>
	IEnumerator DimScreenOverlay(GameObject dimObj, Color dimColor, float lerpTime) {

		float elapsedTime = 0.0f;
		while (elapsedTime < lerpTime) {
			dimObj.renderer.sharedMaterial.color = Color.Lerp(Color.white, dimColor, elapsedTime / lerpTime);
			elapsedTime += Time.deltaTime;
			yield return true;
		}

		yield return false;
	}


}
